// pages/mine/components/plan.js
const http = require("../../../../utils/http.js")
const utils = require("../../../../utils/util.js")

Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    pageIndex: 1,
    pageSize: 10,
    totalCount: 0,
    totalPage: 0,

    routeList: [],
  },
  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行

    },
    detached: function() {
      // 在组件实例被从页面节点树移除时执行

    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    initData() {
      http.get("/route/selectByOpenid", {
          pageIndex: this.data.pageIndex,
          pageSize: this.data.pageSize
        })
        .then(res => {
          let routeList = res.data.data.resultList
          for (let i = 0; i < routeList.length; i++) {
            let date = utils.dateFormat(routeList[i].startDayDate)
            routeList[i].startDayDate = date
          }
          this.setData({
            routeList: routeList,
            totalCount: res.data.data.totalCount,
            totalPage: res.data.data.totalPage
          })
        })
    },
    addData() {
      if (this.data.pageIndex >= this.data.totalPage) {
        return;
      }
      this.setData({
        pageIndex: ++this.data.pageIndex
      })
      http.get("/route/routeList", {
          pageIndex: this.data.pageIndex,
          pageSize: this.data.pageSize
        })
        .then(res => {
          let oldRouteList = this.data.routeList
          let routeList = res.data.data.resultList
          for (let i = 0; i < routeList.length; i++) {
            let date = utils.dateFormat(routeList[i].startDayDate)
            routeList[i].startDayDate = date
          }
          oldRouteList = oldRouteList.concat(routeList)
          this.setData({
            impressionList: oldRouteList,
            totalCount: res.data.data.totalCount,
            totalPage: res.data.data.totalPage
          })
        })
    },
    gotoRoute(e) {
      let routeId = e.currentTarget.dataset.id
      wx.navigateTo({
        url: "/pages/route/detail/detail?id=" + routeId,
      })
    }

  }
})